home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / tar-1_11.lha / tar-1.11.2 / malloc.c < prev    next >
C/C++ Source or Header  |  1993-03-25  |  31KB  |  1,037 lines

  1. /* DO NOT EDIT THIS FILE -- it is automagically generated.  -*- C -*- */
  2.  
  3. #define _MALLOC_INTERNAL
  4.  
  5. /* The malloc headers and source files from the C library follow here.  */
  6.  
  7. /* Declarations for `malloc' and friends.
  8.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  9.           Written May 1989 by Mike Haertel.
  10.  
  11. This library is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU Library General Public License as
  13. published by the Free Software Foundation; either version 2 of the
  14. License, or (at your option) any later version.
  15.  
  16. This library is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. Library General Public License for more details.
  20.  
  21. You should have received a copy of the GNU Library General Public
  22. License along with this library; see the file COPYING.LIB.  If
  23. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26.    The author may be reached (Email) at the address mike@ai.mit.edu,
  27.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  28.  
  29. #ifndef _MALLOC_H
  30.  
  31. #define _MALLOC_H    1
  32.  
  33. #ifdef _MALLOC_INTERNAL
  34. /* Harmless, gets __GNU_LIBRARY__ defined.
  35.    We must do this before #defining size_t and ptrdiff_t
  36.    because <stdio.h> tries to typedef them on some systems.  */
  37. #include <stdio.h>
  38. #endif
  39.  
  40. #ifdef    __cplusplus
  41. extern "C"
  42. {
  43. #endif
  44.  
  45. #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
  46. #undef    __P
  47. #define    __P(args)    args
  48. #undef    __ptr_t
  49. #define    __ptr_t        void *
  50. #else /* Not C++ or ANSI C.  */
  51. #undef    __P
  52. #define    __P(args)    ()
  53. #undef    const
  54. #define    const
  55. #undef    __ptr_t
  56. #define    __ptr_t        char *
  57. #endif /* C++ or ANSI C.  */
  58.  
  59. #ifndef    NULL
  60. #define    NULL    0
  61. #endif
  62.  
  63. #ifdef    __STDC__
  64. #include <stddef.h>
  65. #else
  66. #undef    size_t
  67. #define    size_t        unsigned int
  68. #undef    ptrdiff_t
  69. #define    ptrdiff_t    int
  70. #endif
  71.  
  72.  
  73. /* Allocate SIZE bytes of memory.  */
  74. extern __ptr_t malloc __P ((size_t __size));
  75. /* Re-allocate the previously allocated block
  76.    in __ptr_t, making the new block SIZE bytes long.  */
  77. extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
  78. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
  79. extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
  80. /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
  81. extern void free __P ((__ptr_t __ptr));
  82.  
  83. /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
  84. extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
  85.  
  86. /* Allocate SIZE bytes on a page boundary.  */
  87. extern __ptr_t valloc __P ((size_t __size));
  88.  
  89.  
  90. #ifdef _MALLOC_INTERNAL
  91.  
  92. #ifdef    HAVE_CONFIG_H
  93. #include "config.h"
  94. #endif
  95.  
  96. #if    defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
  97. #include <string.h>
  98. #else
  99. #ifndef memset
  100. #define    memset(s, zero, n)    bzero ((s), (n))
  101. #endif
  102. #ifndef memcpy
  103. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  104. #endif
  105. #ifndef memmove
  106. #define    memmove(d, s, n)    bcopy ((s), (d), (n))
  107. #endif
  108. #endif
  109.  
  110.  
  111. #if    defined(__GNU_LIBRARY__) || defined(__STDC__)
  112. #include <limits.h>
  113. #else
  114. #define    CHAR_BIT    8
  115. #endif
  116.  
  117. /* The allocator divides the heap into blocks of fixed size; large
  118.    requests receive one or more whole blocks, and small requests
  119.    receive a fragment of a block.  Fragment sizes are powers of two,
  120.    and all fragments of a block are the same size.  When all the
  121.    fragments in a block have been freed, the block itself is freed.  */
  122. #define INT_BIT        (CHAR_BIT * sizeof(int))
  123. #define BLOCKLOG    (INT_BIT > 16 ? 12 : 9)
  124. #define BLOCKSIZE    (1 << BLOCKLOG)
  125. #define BLOCKIFY(SIZE)    (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  126.  
  127. /* Determine the amount of memory spanned by the initial heap table
  128.    (not an absolute limit).  */
  129. #define HEAP        (INT_BIT > 16 ? 4194304 : 65536)
  130.  
  131. /* Number of contiguous free blocks allowed to build up at the end of
  132.    memory before they will be returned to the system.  */
  133. #define FINAL_FREE_BLOCKS    8
  134.  
  135. /* Where to start searching the free list when looking for new memory.
  136.    The two possible values are 0 and _heapindex.  Starting at 0 seems
  137.    to reduce total memory usage, while starting at _heapindex seems to
  138.    run faster.  */
  139. #define MALLOC_SEARCH_START    _heapindex
  140.  
  141. /* Data structure giving per-block information.  */
  142. typedef union
  143.   {
  144.     /* Heap information for a busy block.  */
  145.     struct
  146.       {
  147.     /* Zero for a large block, or positive giving the
  148.        logarithm to the base two of the fragment size.  */
  149.     int type;
  150.     union
  151.       {
  152.         struct
  153.           {
  154.         size_t nfree;    /* Free fragments in a fragmented block.  */
  155.         size_t first;    /* First free fragment of the block.  */
  156.           } frag;
  157.         /* Size (in blocks) of a large cluster.  */
  158.         size_t size;
  159.       } info;
  160.       } busy;
  161.     /* Heap information for a free block
  162.        (that may be the first of a free cluster).  */
  163.     struct
  164.       {
  165.     size_t size;        /* Size (in blocks) of a free cluster.  */
  166.     size_t next;        /* Index of next free cluster.  */
  167.     size_t prev;        /* Index of previous free cluster.  */
  168.       } free;
  169.   } malloc_info;
  170.  
  171. /* Pointer to first block of the heap.  */
  172. extern char *_heapbase;
  173.  
  174. /* Table indexed by block number giving per-block information.  */
  175. extern malloc_info *_heapinfo;
  176.  
  177. /* Address to block number and vice versa.  */
  178. #define BLOCK(A)    (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  179. #define ADDRESS(B)    ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  180.  
  181. /* Current search index for the heap table.  */
  182. extern size_t _heapindex;
  183.  
  184. /* Limit of valid info table indices.  */
  185. extern size_t _heaplimit;
  186.  
  187. /* Doubly linked lists of free fragments.  */
  188. struct list
  189.   {
  190.     struct list *next;
  191.     struct list *prev;
  192.   };
  193.  
  194. /* Free list headers for each fragment size.  */
  195. extern struct list _fraghead[];
  196.  
  197. /* List of blocks allocated with `memalign' (or `valloc').  */
  198. struct alignlist
  199.   {
  200.     struct alignlist *next;
  201.     __ptr_t aligned;        /* The address that memaligned returned.  */
  202.     __ptr_t exact;        /* The address that malloc returned.  */
  203.   };
  204. extern struct alignlist *_aligned_blocks;
  205.  
  206. /* Instrumentation.  */
  207. extern size_t _chunks_used;
  208. extern size_t _bytes_used;
  209. extern size_t _chunks_free;
  210. extern size_t _bytes_free;
  211.  
  212. /* Internal version of `free' used in `morecore' (malloc.c). */
  213. extern void _free_internal __P ((__ptr_t __ptr));
  214.  
  215. #endif /* _MALLOC_INTERNAL.  */
  216.  
  217. /* Underlying allocation function; successive calls should
  218.    return contiguous pieces of memory.  */
  219. extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
  220.  
  221. /* Default value of `__morecore'.  */
  222. extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
  223.  
  224. /* If not NULL, this function is called after each time
  225.    `__morecore' is called to increase the data size.  */
  226. extern void (*__after_morecore_hook) __P ((void));
  227.  
  228. /* Nonzero if `malloc' has been called and done its initialization.  */
  229. extern int __malloc_initialized;
  230.  
  231. /* Hooks for debugging versions.  */
  232. extern void (*__free_hook) __P ((__ptr_t __ptr));
  233. extern __ptr_t (*__malloc_hook) __P ((size_t __size));
  234. extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  235.  
  236. /* Activate a standard collection of debugging hooks.  */
  237. extern int mcheck __P ((void (*__func) __P ((void))));
  238.  
  239. /* Activate a standard collection of tracing hooks.  */
  240. extern void mtrace __P ((void));
  241.  
  242. /* Statistics available to the user.  */
  243. struct mstats
  244.   {
  245.     size_t bytes_total;        /* Total size of the heap. */
  246.     size_t chunks_used;        /* Chunks allocated by the user. */
  247.     size_t bytes_used;        /* Byte total of user-allocated chunks. */
  248.     size_t chunks_free;        /* Chunks in the free list. */
  249.     size_t bytes_free;        /* Byte total of chunks in the free list. */
  250.   };
  251.  
  252. /* Pick up the current statistics. */
  253. extern struct mstats mstats __P ((void));
  254.  
  255. /* Call WARNFUN with a warning message when memory usage is high.  */
  256. extern void memory_warnings __P ((__ptr_t __start,
  257.                   void (*__warnfun) __P ((__const char *))));
  258.  
  259.  
  260. /* Relocating allocator.  */
  261.  
  262. /* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
  263. extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
  264.  
  265. /* Free the storage allocated in HANDLEPTR.  */
  266. extern void r_alloc_free __P ((__ptr_t *__handleptr));
  267.  
  268. /* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
  269. extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, size_t __size));
  270.  
  271.  
  272. #ifdef    __cplusplus
  273. }
  274. #endif
  275.  
  276. #endif /* malloc.h  */
  277. /* Memory allocator `malloc'.
  278.    Copyright 1990, 1991, 1992 Free Software Foundation
  279.           Written May 1989 by Mike Haertel.
  280.  
  281. This library is free software; you can redistribute it and/or
  282. modify it under the terms of the GNU Library General Public License as
  283. published by the Free Software Foundation; either version 2 of the
  284. License, or (at your option) any later version.
  285.  
  286. This library is distributed in the hope that it will be useful,
  287. but WITHOUT ANY WARRANTY; without even the implied warranty of
  288. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  289. Library General Public License for more details.
  290.  
  291. You should have received a copy of the GNU Library General Public
  292. License along with this library; see the file COPYING.LIB.  If
  293. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  294. Cambridge, MA 02139, USA.
  295.  
  296.    The author may be reached (Email) at the address mike@ai.mit.edu,
  297.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  298.  
  299. #ifndef    _MALLOC_INTERNAL
  300. #define _MALLOC_INTERNAL
  301. #include <malloc.h>
  302. #endif
  303.  
  304. /* How to really get more memory.  */
  305. __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
  306.  
  307. /* Debugging hook for `malloc'.  */
  308. __ptr_t (*__malloc_hook) __P ((size_t __size));
  309.  
  310. /* Pointer to the base of the first block.  */
  311. char *_heapbase;
  312.  
  313. /* Block information table.  Allocated with align/__free (not malloc/free).  */
  314. malloc_info *_heapinfo;
  315.  
  316. /* Number of info entries.  */
  317. static size_t heapsize;
  318.  
  319. /* Search index in the info table.  */
  320. size_t _heapindex;
  321.  
  322. /* Limit of valid info table indices.  */
  323. size_t _heaplimit;
  324.  
  325. /* Free lists for each fragment size.  */
  326. struct list _fraghead[BLOCKLOG];
  327.  
  328. /* Instrumentation.  */
  329. size_t _chunks_used;
  330. size_t _bytes_used;
  331. size_t _chunks_free;
  332. size_t _bytes_free;
  333.  
  334. /* Are you experienced?  */
  335. int __malloc_initialized;
  336.  
  337. void (*__after_morecore_hook) __P ((void));
  338.  
  339. /* Aligned allocation.  */
  340. static __ptr_t align __P ((size_t));
  341. static __ptr_t
  342. align (size)
  343.      size_t size;
  344. {
  345.   __ptr_t result;
  346.   unsigned long int adj;
  347.  
  348.   result = (*__morecore) (size);
  349.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  350.                         (char *) NULL)) % BLOCKSIZE;
  351.   if (adj != 0)
  352.     {
  353.       adj = BLOCKSIZE - adj;
  354.       (void) (*__morecore) (adj);
  355.       result = (char *) result + adj;
  356.     }
  357.  
  358.   if (__after_morecore_hook)
  359.     (*__after_morecore_hook) ();
  360.  
  361.   return result;
  362. }
  363.  
  364. /* Set everything up and remember that we have.  */
  365. static int initialize __P ((void));
  366. static int
  367. initialize ()
  368. {
  369.   heapsize = HEAP / BLOCKSIZE;
  370.   _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
  371.   if (_heapinfo == NULL)
  372.     return 0;
  373.   memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
  374.   _heapinfo[0].free.size = 0;
  375.   _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  376.   _heapindex = 0;
  377.   _heapbase = (char *) _heapinfo;
  378.   __malloc_initialized = 1;
  379.   return 1;
  380. }
  381.  
  382. /* Get neatly aligned memory, initializing or
  383.    growing the heap info table as necessary. */
  384. static __ptr_t morecore __P ((size_t));
  385. static __ptr_t
  386. morecore (size)
  387.      size_t size;
  388. {
  389.   __ptr_t result;
  390.   malloc_info *newinfo, *oldinfo;
  391.   size_t newsize;
  392.  
  393.   result = align (size);
  394.   if (result == NULL)
  395.     return NULL;
  396.  
  397.   /* Check if we need to grow the info table.  */
  398.   if ((size_t) BLOCK ((char *) result + size) > heapsize)
  399.     {
  400.       newsize = heapsize;
  401.       while ((size_t) BLOCK ((char *) result + size) > newsize)
  402.     newsize *= 2;
  403.       newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
  404.       if (newinfo == NULL)
  405.     {
  406.       (*__morecore) (-size);
  407.       return NULL;
  408.     }
  409.       memset (newinfo, 0, newsize * sizeof (malloc_info));
  410.       memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
  411.       oldinfo = _heapinfo;
  412.       newinfo[BLOCK (oldinfo)].busy.type = 0;
  413.       newinfo[BLOCK (oldinfo)].busy.info.size
  414.     = BLOCKIFY (heapsize * sizeof (malloc_info));
  415.       _heapinfo = newinfo;
  416.       _free_internal (oldinfo);
  417.       heapsize = newsize;
  418.     }
  419.  
  420.   _heaplimit = BLOCK ((char *) result + size);
  421.   return result;
  422. }
  423.  
  424. /* Allocate memory from the heap.  */
  425. __ptr_t
  426. malloc (size)
  427.      size_t size;
  428. {
  429.   __ptr_t result;
  430.   size_t block, blocks, lastblocks, start;
  431.   register size_t i;
  432.   struct list *next;
  433.  
  434.   if (size == 0)
  435.     return NULL;
  436.  
  437.   if (__malloc_hook != NULL)
  438.     return (*__malloc_hook) (size);
  439.  
  440.   if (!__malloc_initialized)
  441.     if (!initialize ())
  442.       return NULL;
  443.  
  444.   if (size < sizeof (struct list))
  445.       size = sizeof (struct list);
  446.  
  447.   /* Determine the allocation policy based on the request size.  */
  448.   if (size <= BLOCKSIZE / 2)
  449.     {
  450.       /* Small allocation to receive a fragment of a block.
  451.      Determine the logarithm to base two of the fragment size. */
  452.       register size_t log = 1;
  453.       --size;
  454.       while ((size /= 2) != 0)
  455.     ++log;
  456.  
  457.       /* Look in the fragment lists for a
  458.      free fragment of the desired size. */
  459.       next = _fraghead[log].next;
  460.       if (next != NULL)
  461.     {
  462.       /* There are free fragments of this size.
  463.          Pop a fragment out of the fragment list and return it.
  464.          Update the block's nfree and first counters. */
  465.       result = (__ptr_t) next;
  466.       next->prev->next = next->next;
  467.       if (next->next != NULL)
  468.         next->next->prev = next->prev;
  469.       block = BLOCK (result);
  470.       if (--_heapinfo[block].busy.info.frag.nfree != 0)
  471.         _heapinfo[block].busy.info.frag.first = (unsigned long int)
  472.           ((unsigned long int) ((char *) next->next - (char *) NULL)
  473.            % BLOCKSIZE) >> log;
  474.  
  475.       /* Update the statistics.  */
  476.       ++_chunks_used;
  477.       _bytes_used += 1 << log;
  478.       --_chunks_free;
  479.       _bytes_free -= 1 << log;
  480.     }
  481.       else
  482.     {
  483.       /* No free fragments of the desired size, so get a new block
  484.          and break it into fragments, returning the first.  */
  485.       result = malloc (BLOCKSIZE);
  486.       if (result == NULL)
  487.         return NULL;
  488.  
  489.       /* Link all fragments but the first into the free list.  */
  490.       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i)
  491.         {
  492.           next = (struct list *) ((char *) result + (i << log));
  493.           next->next = _fraghead[log].next;
  494.           next->prev = &_fraghead[log];
  495.           next->prev->next = next;
  496.           if (next->next != NULL)
  497.         next->next->prev = next;
  498.         }
  499.  
  500.       /* Initialize the nfree and first counters for this block.  */
  501.       block = BLOCK (result);
  502.       _heapinfo[block].busy.type = log;
  503.       _heapinfo[block].busy.info.frag.nfree = i - 1;
  504.       _heapinfo[block].busy.info.frag.first = i - 1;
  505.  
  506.       _chunks_free += (BLOCKSIZE >> log) - 1;
  507.       _bytes_free += BLOCKSIZE - (1 << log);
  508.       _bytes_used -= BLOCKSIZE - (1 << log);
  509.     }
  510.     }
  511.   else
  512.     {
  513.       /* Large allocation to receive one or more blocks.
  514.      Search the free list in a circle starting at the last place visited.
  515.      If we loop completely around without finding a large enough
  516.      space we will have to get more memory from the system.  */
  517.       blocks = BLOCKIFY (size);
  518.       start = block = MALLOC_SEARCH_START;
  519.       while (_heapinfo[block].free.size < blocks)
  520.     {
  521.       block = _heapinfo[block].free.next;
  522.       if (block == start)
  523.         {
  524.           /* Need to get more from the system.  Check to see if
  525.          the new core will be contiguous with the final free
  526.          block; if so we don't need to get as much.  */
  527.           block = _heapinfo[0].free.prev;
  528.           lastblocks = _heapinfo[block].free.size;
  529.           if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
  530.           (*__morecore) (0) == ADDRESS (block + lastblocks) &&
  531.           (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
  532.         {
  533.           _heapinfo[block].free.size = blocks;
  534.           _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
  535.           continue;
  536.         }
  537.           result = morecore (blocks * BLOCKSIZE);
  538.           if (result == NULL)
  539.         return NULL;
  540.           block = BLOCK (result);
  541.           _heapinfo[block].busy.type = 0;
  542.           _heapinfo[block].busy.info.size = blocks;
  543.           ++_chunks_used;
  544.           _bytes_used += blocks * BLOCKSIZE;
  545.           return result;
  546.         }
  547.     }
  548.  
  549.       /* At this point we have found a suitable free list entry.
  550.      Figure out how to remove what we need from the list. */
  551.       result = ADDRESS (block);
  552.       if (_heapinfo[block].free.size > blocks)
  553.     {
  554.       /* The block we found has a bit left over,
  555.          so relink the tail end back into the free list. */
  556.       _heapinfo[block + blocks].free.size
  557.         = _heapinfo[block].free.size - blocks;
  558.       _heapinfo[block + blocks].free.next
  559.         = _heapinfo[block].free.next;
  560.       _heapinfo[block + blocks].free.prev
  561.         = _heapinfo[block].free.prev;
  562.       _heapinfo[_heapinfo[block].free.prev].free.next
  563.         = _heapinfo[_heapinfo[block].free.next].free.prev
  564.         = _heapindex = block + blocks;
  565.     }
  566.       else
  567.     {
  568.       /* The block exactly matches our requirements,
  569.          so just remove it from the list. */
  570.       _heapinfo[_heapinfo[block].free.next].free.prev
  571.         = _heapinfo[block].free.prev;
  572.       _heapinfo[_heapinfo[block].free.prev].free.next
  573.         = _heapindex = _heapinfo[block].free.next;
  574.       --_chunks_free;
  575.     }
  576.  
  577.       _heapinfo[block].busy.type = 0;
  578.       _heapinfo[block].busy.info.size = blocks;
  579.       ++_chunks_used;
  580.       _bytes_used += blocks * BLOCKSIZE;
  581.       _bytes_free -= blocks * BLOCKSIZE;
  582.     }
  583.  
  584.   return result;
  585. }
  586. /* Free a block of memory allocated by `malloc'.
  587.    Copyright 1990, 1991, 1992 Free Software Foundation
  588.           Written May 1989 by Mike Haertel.
  589.  
  590. This library is free software; you can redistribute it and/or
  591. modify it under the terms of the GNU Library General Public License as
  592. published by the Free Software Foundation; either version 2 of the
  593. License, or (at your option) any later version.
  594.  
  595. This library is distributed in the hope that it will be useful,
  596. but WITHOUT ANY WARRANTY; without even the implied warranty of
  597. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  598. Library General Public License for more details.
  599.  
  600. You should have received a copy of the GNU Library General Public
  601. License along with this library; see the file COPYING.LIB.  If
  602. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  603. Cambridge, MA 02139, USA.
  604.  
  605.    The author may be reached (Email) at the address mike@ai.mit.edu,
  606.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  607.  
  608. #ifndef    _MALLOC_INTERNAL
  609. #define _MALLOC_INTERNAL
  610. #include <malloc.h>
  611. #endif
  612.  
  613. /* Debugging hook for free.  */
  614. void (*__free_hook) __P ((__ptr_t __ptr));
  615.  
  616. /* List of blocks allocated by memalign.  */
  617. struct alignlist *_aligned_blocks = NULL;
  618.  
  619. /* Return memory to the heap.
  620.    Like `free' but don't call a __free_hook if there is one.  */
  621. void
  622. _free_internal (ptr)
  623.      __ptr_t ptr;
  624. {
  625.   int type;
  626.   size_t block, blocks;
  627.   register size_t i;
  628.   struct list *prev, *next;
  629.  
  630.   block = BLOCK (ptr);
  631.  
  632.   type = _heapinfo[block].busy.type;
  633.   switch (type)
  634.     {
  635.     case 0:
  636.       /* Get as many statistics as early as we can.  */
  637.       --_chunks_used;
  638.       _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
  639.       _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
  640.  
  641.       /* Find the free cluster previous to this one in the free list.
  642.      Start searching at the last block referenced; this may benefit
  643.      programs with locality of allocation.  */
  644.       i = _heapindex;
  645.       if (i > block)
  646.     while (i > block)
  647.       i = _heapinfo[i].free.prev;
  648.       else
  649.     {
  650.       do
  651.         i = _heapinfo[i].free.next;
  652.       while (i > 0 && i < block);
  653.       i = _heapinfo[i].free.prev;
  654.     }
  655.  
  656.       /* Determine how to link this block into the free list.  */
  657.       if (block == i + _heapinfo[i].free.size)
  658.     {
  659.       /* Coalesce this block with its predecessor.  */
  660.       _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
  661.       block = i;
  662.     }
  663.       else
  664.     {
  665.       /* Really link this block back into the free list.  */
  666.       _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
  667.       _heapinfo[block].free.next = _heapinfo[i].free.next;
  668.       _heapinfo[block].free.prev = i;
  669.       _heapinfo[i].free.next = block;
  670.       _heapinfo[_heapinfo[block].free.next].free.prev = block;
  671.       ++_chunks_free;
  672.     }
  673.  
  674.       /* Now that the block is linked in, see if we can coalesce it
  675.      with its successor (by deleting its successor from the list
  676.      and adding in its size).  */
  677.       if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
  678.     {
  679.       _heapinfo[block].free.size
  680.         += _heapinfo[_heapinfo[block].free.next].free.size;
  681.       _heapinfo[block].free.next
  682.         = _heapinfo[_heapinfo[block].free.next].free.next;
  683.       _heapinfo[_heapinfo[block].free.next].free.prev = block;
  684.       --_chunks_free;
  685.     }
  686.  
  687.       /* Now see if we can return stuff to the system.  */
  688.       blocks = _heapinfo[block].free.size;
  689.       if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
  690.       && (*__morecore) (0) == ADDRESS (block + blocks))
  691.     {
  692.       register size_t bytes = blocks * BLOCKSIZE;
  693.       _heaplimit -= blocks;
  694.       (*__morecore) (-bytes);
  695.       _heapinfo[_heapinfo[block].free.prev].free.next
  696.         = _heapinfo[block].free.next;
  697.       _heapinfo[_heapinfo[block].free.next].free.prev
  698.         = _heapinfo[block].free.prev;
  699.       block = _heapinfo[block].free.prev;
  700.       --_chunks_free;
  701.       _bytes_free -= bytes;
  702.     }
  703.  
  704.       /* Set the next search to begin at this block.  */
  705.       _heapindex = block;
  706.       break;
  707.  
  708.     default:
  709.       /* Do some of the statistics.  */
  710.       --_chunks_used;
  711.       _bytes_used -= 1 << type;
  712.       ++_chunks_free;
  713.       _bytes_free += 1 << type;
  714.  
  715.       /* Get the address of the first free fragment in this block.  */
  716.       prev = (struct list *) ((char *) ADDRESS (block) +
  717.                (_heapinfo[block].busy.info.frag.first << type));
  718.  
  719.       if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
  720.     {
  721.       /* If all fragments of this block are free, remove them
  722.          from the fragment list and free the whole block.  */
  723.       next = prev;
  724.       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i)
  725.         next = next->next;
  726.       prev->prev->next = next;
  727.       if (next != NULL)
  728.         next->prev = prev->prev;
  729.       _heapinfo[block].busy.type = 0;
  730.       _heapinfo[block].busy.info.size = 1;
  731.  
  732.       /* Keep the statistics accurate.  */
  733.       ++_chunks_used;
  734.       _bytes_used += BLOCKSIZE;
  735.       _chunks_free -= BLOCKSIZE >> type;
  736.       _bytes_free -= BLOCKSIZE;
  737.  
  738.       free (ADDRESS (block));
  739.     }
  740.       else if (_heapinfo[block].busy.info.frag.nfree != 0)
  741.     {
  742.       /* If some fragments of this block are free, link this
  743.          fragment into the fragment list after the first free
  744.          fragment of this block. */
  745.       next = (struct list *) ptr;
  746.       next->next = prev->next;
  747.       next->prev = prev;
  748.       prev->next = next;
  749.       if (next->next != NULL)
  750.         next->next->prev = next;
  751.       ++_heapinfo[block].busy.info.frag.nfree;
  752.     }
  753.       else
  754.     {
  755.       /* No fragments of this block are free, so link this
  756.          fragment into the fragment list and announce that
  757.          it is the first free fragment of this block. */
  758.       prev = (struct list *) ptr;
  759.       _heapinfo[block].busy.info.frag.nfree = 1;
  760.       _heapinfo[block].busy.info.frag.first = (unsigned long int)
  761.         ((unsigned long int) ((char *) ptr - (char *) NULL)
  762.          % BLOCKSIZE >> type);
  763.       prev->next = _fraghead[type].next;
  764.       prev->prev = &_fraghead[type];
  765.       prev->prev->next = prev;
  766.       if (prev->next != NULL)
  767.         prev->next->prev = prev;
  768.     }
  769.       break;
  770.     }
  771. }
  772.  
  773. /* Return memory to the heap.  */
  774. void
  775. free (ptr)
  776.      __ptr_t ptr;
  777. {
  778.   register struct alignlist *l;
  779.  
  780.   if (ptr == NULL)
  781.     return;
  782.  
  783.   for (l = _aligned_blocks; l != NULL; l = l->next)
  784.     if (l->aligned == ptr)
  785.       {
  786.     l->aligned = NULL;    /* Mark the slot in the list as free.  */
  787.     ptr = l->exact;
  788.     break;
  789.       }
  790.  
  791.   if (__free_hook != NULL)
  792.     (*__free_hook) (ptr);
  793.   else
  794.     _free_internal (ptr);
  795. }
  796. /* Change the size of a block allocated by `malloc'.
  797.    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
  798.              Written May 1989 by Mike Haertel.
  799.  
  800. This library is free software; you can redistribute it and/or
  801. modify it under the terms of the GNU Library General Public License as
  802. published by the Free Software Foundation; either version 2 of the
  803. License, or (at your option) any later version.
  804.  
  805. This library is distributed in the hope that it will be useful,
  806. but WITHOUT ANY WARRANTY; without even the implied warranty of
  807. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  808. Library General Public License for more details.
  809.  
  810. You should have received a copy of the GNU Library General Public
  811. License along with this library; see the file COPYING.LIB.  If
  812. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  813. Cambridge, MA 02139, USA.
  814.  
  815.    The author may be reached (Email) at the address mike@ai.mit.edu,
  816.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  817.  
  818. #ifndef    _MALLOC_INTERNAL
  819. #define _MALLOC_INTERNAL
  820. #include <malloc.h>
  821. #endif
  822.  
  823. #define min(A, B) ((A) < (B) ? (A) : (B))
  824.  
  825. /* Debugging hook for realloc.  */
  826. __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  827.  
  828. /* Resize the given region to the new size, returning a pointer
  829.    to the (possibly moved) region.  This is optimized for speed;
  830.    some benchmarks seem to indicate that greater compactness is
  831.    achieved by unconditionally allocating and copying to a
  832.    new region.  This module has incestuous knowledge of the
  833.    internals of both free and malloc. */
  834. __ptr_t
  835. realloc (ptr, size)
  836.      __ptr_t ptr;
  837.      size_t size;
  838. {
  839.   __ptr_t result;
  840.   int type;
  841.   size_t block, blocks, oldlimit;
  842.  
  843.   if (size == 0)
  844.     {
  845.       free (ptr);
  846.       return malloc (0);
  847.     }
  848.   else if (ptr == NULL)
  849.     return malloc (size);
  850.  
  851.   if (__realloc_hook != NULL)
  852.     return (*__realloc_hook) (ptr, size);
  853.  
  854.   block = BLOCK (ptr);
  855.  
  856.   type = _heapinfo[block].busy.type;
  857.   switch (type)
  858.     {
  859.     case 0:
  860.       /* Maybe reallocate a large block to a small fragment.  */
  861.       if (size <= BLOCKSIZE / 2)
  862.     {
  863.       result = malloc (size);
  864.       if (result != NULL)
  865.         {
  866.           memcpy (result, ptr, size);
  867.           free (ptr);
  868.           return result;
  869.         }
  870.     }
  871.  
  872.       /* The new size is a large allocation as well;
  873.      see if we can hold it in place. */
  874.       blocks = BLOCKIFY (size);
  875.       if (blocks < _heapinfo[block].busy.info.size)
  876.     {
  877.       /* The new size is smaller; return
  878.          excess memory to the free list. */
  879.       _heapinfo[block + blocks].busy.type = 0;
  880.       _heapinfo[block + blocks].busy.info.size
  881.         = _heapinfo[block].busy.info.size - blocks;
  882.       _heapinfo[block].busy.info.size = blocks;
  883.       free (ADDRESS (block + blocks));
  884.       result = ptr;
  885.     }
  886.       else if (blocks == _heapinfo[block].busy.info.size)
  887.     /* No size change necessary.  */
  888.     result = ptr;
  889.       else
  890.     {
  891.       /* Won't fit, so allocate a new region that will.
  892.          Free the old region first in case there is sufficient
  893.          adjacent free space to grow without moving. */
  894.       blocks = _heapinfo[block].busy.info.size;
  895.       /* Prevent free from actually returning memory to the system.  */
  896.       oldlimit = _heaplimit;
  897.       _heaplimit = 0;
  898.       free (ptr);
  899.       _heaplimit = oldlimit;
  900.       result = malloc (size);
  901.       if (result == NULL)
  902.         {
  903.           (void) malloc (blocks * BLOCKSIZE);
  904.           return NULL;
  905.         }
  906.       if (ptr != result)
  907.         memmove (result, ptr, blocks * BLOCKSIZE);
  908.     }
  909.       break;
  910.  
  911.     default:
  912.       /* Old size is a fragment; type is logarithm
  913.      to base two of the fragment size.  */
  914.       if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
  915.     /* The new size is the same kind of fragment.  */
  916.     result = ptr;
  917.       else
  918.     {
  919.       /* The new size is different; allocate a new space,
  920.          and copy the lesser of the new size and the old. */
  921.       result = malloc (size);
  922.       if (result == NULL)
  923.         return NULL;
  924.       memcpy (result, ptr, min (size, (size_t) 1 << type));
  925.       free (ptr);
  926.     }
  927.       break;
  928.     }
  929.  
  930.   return result;
  931. }
  932. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  933. This file is part of the GNU C Library.
  934.  
  935. The GNU C Library is free software; you can redistribute it and/or modify
  936. it under the terms of the GNU General Public License as published by
  937. the Free Software Foundation; either version 2, or (at your option)
  938. any later version.
  939.  
  940. The GNU C Library is distributed in the hope that it will be useful,
  941. but WITHOUT ANY WARRANTY; without even the implied warranty of
  942. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  943. GNU General Public License for more details.
  944.  
  945. You should have received a copy of the GNU General Public License
  946. along with the GNU C Library; see the file COPYING.  If not, write to
  947. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  948.  
  949. #ifndef    _MALLOC_INTERNAL
  950. #define    _MALLOC_INTERNAL
  951. #include <malloc.h>
  952. #endif
  953.  
  954. #ifndef    __GNU_LIBRARY__
  955. #define    __sbrk    sbrk
  956. #endif
  957.  
  958. extern __ptr_t __sbrk __P ((int increment));
  959.  
  960. #ifndef NULL
  961. #define NULL 0
  962. #endif
  963.  
  964. /* Allocate INCREMENT more bytes of data space,
  965.    and return the start of data space, or NULL on errors.
  966.    If INCREMENT is negative, shrink data space.  */
  967. __ptr_t
  968. __default_morecore (increment)
  969.      ptrdiff_t increment;
  970. {
  971.   __ptr_t result = __sbrk ((int) increment);
  972.   if (result == (__ptr_t) -1)
  973.     return NULL;
  974.   return result;
  975. }
  976. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  977.  
  978. This library is free software; you can redistribute it and/or
  979. modify it under the terms of the GNU Library General Public License as
  980. published by the Free Software Foundation; either version 2 of the
  981. License, or (at your option) any later version.
  982.  
  983. This library is distributed in the hope that it will be useful,
  984. but WITHOUT ANY WARRANTY; without even the implied warranty of
  985. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  986. Library General Public License for more details.
  987.  
  988. You should have received a copy of the GNU Library General Public
  989. License along with this library; see the file COPYING.LIB.  If
  990. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  991. Cambridge, MA 02139, USA.  */
  992.  
  993. #ifndef    _MALLOC_INTERNAL
  994. #define _MALLOC_INTERNAL
  995. #include <malloc.h>
  996. #endif
  997.  
  998. __ptr_t
  999. memalign (alignment, size)
  1000.      size_t alignment;
  1001.      size_t size;
  1002. {
  1003.   __ptr_t result;
  1004.   unsigned long int adj;
  1005.  
  1006.   size = ((size + alignment - 1) / alignment) * alignment;
  1007.  
  1008.   result = malloc (size);
  1009.   if (result == NULL)
  1010.     return NULL;
  1011.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  1012.                         (char *) NULL)) % alignment;
  1013.   if (adj != 0)
  1014.     {
  1015.       struct alignlist *l;
  1016.       for (l = _aligned_blocks; l != NULL; l = l->next)
  1017.     if (l->aligned == NULL)
  1018.       /* This slot is free.  Use it.  */
  1019.       break;
  1020.       if (l == NULL)
  1021.     {
  1022.       l = (struct alignlist *) malloc (sizeof (struct alignlist));
  1023.       if (l == NULL)
  1024.         {
  1025.           free (result);
  1026.           return NULL;
  1027.         }
  1028.     }
  1029.       l->exact = result;
  1030.       result = l->aligned = (char *) result + alignment - adj;
  1031.       l->next = _aligned_blocks;
  1032.       _aligned_blocks = l;
  1033.     }
  1034.  
  1035.   return result;
  1036. }
  1037.